package light.head.dao.el;

import java.util.List;

import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.el.Classes;
import light.head.model.el.ClassesCourseRelation;
import light.head.model.el.ClassesCourseRelationPK;

import org.springframework.stereotype.Service;
/**
 * 班级课程关系DAO
 * @author yuleidian
 *
 */
@Service
public class ClassesCourseRelationDAO extends BaseDAO<ClassesCourseRelation, ClassesCourseRelationPK>{
	private final String hql_findByCcrCode = "FROM ClassesCourseRelation ccr WHERE ccr.clc = ?";
	private final String hql_findByCode = "FROM Classes c WHERE c.code = ?";
	private final String hql_findByCcAndClc = "FROM ClassesCourseRelation ccr WHERE ccr.cc = ? AND ccr.clc = ? ";
	/**
	 * 根据班级 code 查询信息
	 * @return
	 */
	public List<ClassesCourseRelation> findByCcrCode(Integer code){
		if(code == null){
			 return null;
		}
		return find(hql_findByCcrCode,code);
	}
	public ClassesCourseRelation hql_findByCcAndClc(Integer cc ,Integer clc){
		return (ClassesCourseRelation)get(hql_findByCcAndClc,cc,clc);
	}
	public Classes hql_findByClc(Integer clc){
		return (Classes)get(hql_findByCode,clc);
	}
	/**
	 * 更新
	 */
	public RESULT update(ClassesCourseRelation ccr){
		if(ccr.getCc() == null){
			return RESULT.ERROR;
		}
		ClassesCourseRelation old = hql_findByCcAndClc(ccr.getCc(),ccr.getClc());
		if(ccr.getExamTime() != null && ccr.getExamTime() != old.getExamTime()){
			old.setExamTime(ccr.getExamTime());
		}
		if(ccr.getTime() != null && ccr.getTime() != old.getTime()){
			old.setTime(ccr.getTime());
		}
		if(ccr.getNum() != null && ccr.getNum() != old.getNum()){
			old.setNum(ccr.getNum());
		}
		if(ccr.getQualifiedSocre() != null && ccr.getQualifiedSocre() != old.getQualifiedSocre()){
			old.setQualifiedSocre(ccr.getQualifiedSocre());
		}
		if(ccr.getTtc() != null && ccr.getTtc() != old.getTtc()){
			old.setTtc(ccr.getTtc());
		}
		if(ccr.getType() != null && ccr.getType() != old.getType()){
			old.setType(ccr.getType());
		}
		return upd(old);
	}
}	
